package com.fzk.blog.settings.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fzk.blog.bean.Role;
import com.fzk.blog.bean.vo.RoleInfo;
import com.fzk.blog.exception.MyException;
import com.fzk.blog.settings.service.IRoleService;
import com.fzk.blog.utils.ResponseResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author fzk
 * @date 2021-08-18 0:52
 */
@RestController
public class RoleController {
    @Resource
    private IRoleService roleService;

    @GetMapping(path = "/role/info")
    public Map<String, Object> getRoleInfoListOrOne(
            @RequestParam(name = "id", required = false) String roleId
    ) throws JsonProcessingException {
        // 1.检查参数
        if (Strings.trimToNull(roleId) != null) {
            // 2.1 查询特定roleInfo
            RoleInfo roleInfo = roleService.getRoleInfoById(Integer.parseInt(roleId));
            return ResponseResult.success("查询成功", roleInfo);
        } else {
            // 2.2 查询roleInfoList
            List<RoleInfo> roleInfoList = roleService.getRoleInfoList();
            return ResponseResult.success("查询成功", roleInfoList);
        }
    }

    @PostMapping(path = "/role")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> insertRole(
            @RequestParam("name") String name,
            @RequestParam("description") String description
    ) {
        // 1.检查参数
        checkArgs(name, description);

        // 2.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 3.装入参数
        Role role = new Role();
        role.setName(name);
        role.setDescription(description);

        // 4.调用service
        RoleInfo roleInfo = roleService.insertRole(role, loginId);

        return ResponseResult.success("添加成功", roleInfo);
    }

    @PutMapping(path = "/role")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> updateRole(
            @RequestParam("id") int roleId,
            @RequestParam("name") String name,
            @RequestParam("description") String description
    ) throws JsonProcessingException {
        // 1.检查参数
        checkArgs(name, description);

        // 2.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 3.装入参数
        Role role = new Role();
        role.setId(roleId);
        role.setName(name);
        role.setDescription(description);

        // 4.调用service
        RoleInfo roleInfo = roleService.updateRole(role, loginId);

        return ResponseResult.success("修改成功", roleInfo);
    }

    @DeleteMapping(path = "/role")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> deleteRole(
            @RequestParam("id") int roleId
    ) {

        // 1.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 2.调用service
        RoleInfo roleInfo = roleService.deleteRole(roleId, loginId);

        return ResponseResult.success("删除成功", roleInfo);
    }

    /**
     * 参数检查
     *
     * @param args 可变形参
     */
    private void checkArgs(String... args) {
        for (String arg : args) {
            if (arg == null || arg.length() == 0)
                throw new MyException(400, "参数错误：" + arg);
        }
    }
}
